package com.kgcx.coupon.controller;

import com.kgcx.common.annotation.validator.Validator;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.constant.ResultCode;
import com.kgcx.common.controller.BaseController;
import com.kgcx.coupon.domain.Billing;
import com.kgcx.coupon.service.BillingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "发票接口", tags = "发票接口")
@RestController
@RequestMapping("billing")
public class BillingController extends BaseController {

    @Autowired
    private BillingService billingService;

    @ApiOperation(value = "后台根据id查询发票", httpMethod = "GET", notes = "这是用来查询发票的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "主键id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
    })
    @GetMapping("selectPrimaryKey")
    public ReturnData<Billing> selectPrimaryKey() {
        RequestData requestData = this.getRequestData();
        Billing billing = billingService.selectPrimaryKey(requestData.getLong("kid"));
        return new ReturnData<>(billing);
    }




    @ApiOperation(value = "后台分页查询所有发票", httpMethod = "GET", notes = "这是用来查询发票的接口")
    @ApiImplicitParams({


            @ApiImplicitParam(name = "title", value = "抬头", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "billingType", value = "类型", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderStatus", value = "状态", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderTimeBegin", value = "时间起", required = false, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orderTimeEnd", value = "时间终", required = false, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("select")
    public ReturnData<List<Billing>> select() {
        RequestData requestData = this.getRequestData();
        List<Billing> couponList = billingService.select(requestData);
        return new ReturnData<>(couponList);
    }

    @ApiOperation(value = "新增发票", httpMethod = "POST", notes = "这是用来新增发票的接口")
    @PostMapping("addBilling")
    public ReturnData addBilling(@RequestBody Billing billing) {
        billingService.addBilling(billing);
        return new ReturnData();
    }

    @ApiOperation(value = "后台根据kid删除发票", httpMethod = "POST", notes = "这是用来删除发票的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "主键ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class),
    })
    @PostMapping("deletePrimaryKey")
    public ReturnData deletePrimaryKey(@RequestParam Long kid) {
        billingService.deletePrimaryKey(kid);
        return new ReturnData();
    }

    @ApiOperation(value = "后台修改所有发票", httpMethod = "POST", notes = "这是用来修改发票的接口")
    @PostMapping("updatePrimaryKey")
    public ReturnData updatePrimaryKey(@RequestBody Billing billing) {
        billingService.updatePrimaryKey(billing);
        return new ReturnData();
    }


    @Validator
    @ApiOperation(value = "前台查询用户开票历史", httpMethod = "GET", notes = "开票历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNo", value = "分页号", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "分页行数", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
    })
    @GetMapping("queryUserBilling")
    public ReturnData<List<Billing>> queryUserBilling(Integer userId, Integer pageNo, Integer pageSize) {
        ReturnData<List<Billing>> returnData = new ReturnData<>();
        List<Billing> billings = null;
        try {
            billings = billingService.queryUserBilling(userId, pageNo, pageSize);
            if (billings != null) {
                returnData.setCode(ResultCode.SUCCESS);
                returnData.setMsg("查询成功");
                returnData.setData(billings);
            } else {
                returnData.setCode(ResultCode.VAULE_NULL);
                returnData.setMsg("未有发票历史");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("queryUserBilling error", e);
            returnData.setMsg("查询失败");
            returnData.setCode(ResultCode.SYSTEM_ERROR);
        }
        return returnData;
    }

    @Validator
    @ApiOperation(value = "前台查询用户可开票金额", httpMethod = "GET", notes = "可开票金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
    })
    @GetMapping("getUnBillingAmount")
    public ReturnData<Long> getUnBillingAmount(Integer userId) {
        return new ReturnData<>(billingService.getUnBillingAmount(userId));
    }

    @Validator
    @ApiOperation(value = "发票审核通过", httpMethod = "POST", notes = "发票审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billingId", value = "发票id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
    })
    @PostMapping("approveBilling")
    public ReturnData approveBilling(Long billingId, Long userId) {
        billingService.approveBilling(billingId, userId);
        return new ReturnData();
    }

    @Validator
    @ApiOperation(value = "实际开票", httpMethod = "POST", notes = "改变发票单的状态为开票")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billingId", value = "发票id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
    })
    @PostMapping("billing")
    public ReturnData billing(Long billingId, Long userId) {
        billingService.billing(billingId, userId);
        return new ReturnData();
    }

    @Validator
    @ApiOperation(value = "寄送", httpMethod = "POST", notes = "寄送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billingId", value = "发票id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "mailNumber", value = "快递单号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
    })
    @PostMapping("send")
    public ReturnData send(Long billingId, Long userId, String mailNumber) {
        billingService.send(billingId, userId, mailNumber);
        return new ReturnData();
    }
    @Validator
    @ApiOperation(value = "审核不通过", httpMethod = "POST", notes = "审核不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billingId", value = "发票id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),

    })
    @PostMapping("refuse")
    public ReturnData refuse(Long billingId, Long userId) {
        billingService.refuse(billingId, userId);
        return new ReturnData();
    }


}
